//生成路由
function menusExpansionMap(menu) {
  if (!menu.child) { return menu.path }
  let root = menu;
  let parent = root;
  let child, tmpChild, tmpInd, tmpMap;
  let [menuMap, menuArr, ind, stop, samePath] =
    [new Map(), [], [0], false, []]

  while (!stop) {
    child = getMenu(root, ind);
    tmpInd = ind.concat();
    if (!child) {//child不存在
      //End
      ind[ind.length - 1] >= parent.child.length && ind.length <= 1 &&
        (stop = true);
      //父级弟
      ind.pop();
      ind[ind.length - 1]++;
    } else {//child存在
      //深拷贝(JSON拷贝有坑，比如丢失函数，需要补上)
      tmpChild = JSON.parse(JSON.stringify(child));

      //检查名称重复
      tmpChild.name = globalFnWP.autoReName(tmpChild.name, menuMap);

      //区分level和link
      if (child.imports.slice(-4) == ".vue") {//links
        //path
        !child.path &&
          (tmpChild.path = "/" + child.imports.replace(/.vue$/, ""));

        //检查路径重复
        tmpChild.path = globalFnWP.autoReName(tmpChild.path, samePath);

        //component
        if (!child.component) {
          let tmpTex = getMenuPath(root, ind);
          tmpChild.component = () => import(`@/views/${tmpTex}`);
        } else {
          tmpChild.component = child.component;
        }

        //meta
        let tmpMeta = getMenuMeta(root, ind);
        tmpMeta && (tmpChild.meta = tmpMeta);

        //写入原有信息
        menuArr.push(tmpChild);
        ind[ind.length - 1]++;
        tmpChild.type = "links";
        samePath.push(tmpChild.path);

      } else {//levels
        //showLev
        if (tmpChild.showLev == undefined) {
          tmpChild.showLev = getMenuShowLev(root, ind);
        }

        child.child && child.child.length > 0 ?
          (ind.push(0), parent = child) :
          ind[ind.length - 1]++;
        tmpChild.type = "levels";
      }

      //赋值tmpMap
      tmpMap = {
        ind: tmpInd.concat(),
        type: tmpChild.type,
      }

      //showLev格式
      tmpChild.showLev != undefined && (tmpMap.showLev = tmpChild.showLev);

      //mark
      tmpChild.mark != undefined && (tmpMap.mark = tmpChild.mark);

      //linkTex
      tmpChild.linkTex != undefined ? 
      (tmpMap.linkTex = tmpChild.linkTex)
      :(tmpMap.linkTex = tmpChild.name);

      //写入Map
      menuMap.set(tmpChild.name, tmpMap);
    }
  }

  return {
    menuArr: menuArr,
    menuMap: menuMap
  };
}

function getMenu(item, ind) {
  for (let j = 0; j < ind.length; j++) { item = item.child[ind[j]]; }
  return item;
}

function getMenuPath(item, ind) {
  let path = "";
  for (let j = 0; j < ind.length; j++) {
    item = item.child[ind[j]];
    path += item.imports;
  }
  return path;
}

function getMenuMeta(item, ind) {
  let meta = {};
  for (let j = 0; j < ind.length; j++) {
    item = item.child[ind[j]];
    item.meta &&
      (item.meta.title && (meta.title = (item.meta.title)),
        item.meta.data &&
          meta.data ?
          meta.data = meta.data.concat(item.meta.data) :
          meta.data = item.meta.data);
  }

  //后写覆盖先写
  if (meta.data) {
    let item, itemR;
    for (let i = 0; i < meta.data.length; i++) {
      item = meta.data[i];
      for (let j = i + 1; j < meta.data.length; j++) {
        itemR = meta.data[j];
        Object.keys(item)[0] == Object.keys(itemR)[0] &&
          (meta.data.splice(i, 1), i--);
      }
    }
  }

  Object.keys(meta).length == 0 && (meta = undefined);

  return meta;
}

function getMenuShowLev(item, ind) {
  let showLev;
  for (let j = 0; j < ind.length; j++) {
    item = item.child[ind[j]];
    item.showChi != undefined && (showLev = item.showChi);
  }
  return showLev;
}

export {
  menusExpansionMap
}